package com.meteor.dao.user;

import com.meteor.dao.BaseDao;
import com.meteor.pojo.User;

import java.sql.*;

public class UserDaoImpl implements UserDao{
    public User getLoginUser(Connection connection, String userCode) {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;

        String sql = "select * from `smbms_user` where userCode=?";
        Object[] params = {userCode};

        try {
            resultSet = BaseDao.execute(connection, sql, params, resultSet, preparedStatement);
            if(resultSet.next()) {
                user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUserCode(resultSet.getString("userCode"));
                user.setUserName(resultSet.getString("userName"));
                user.setUserPassword(resultSet.getString("userPassword"));
                user.setGender(resultSet.getInt("gender"));
                user.setBirthday(resultSet.getDate("birthday"));
                user.setPhone(resultSet.getString("phone"));
                user.setAddress(resultSet.getString("address"));
                user.setUserRole(resultSet.getInt("userRole"));
                user.setCreateBy(resultSet.getInt("createdBy"));
                user.setCreationDate(resultSet.getDate("creationDate"));
                user.setModifyBy(resultSet.getInt("modifyBy"));
                user.setModifyDate(resultSet.getDate("modifyDate"));
            }
            BaseDao.closeResource(null,preparedStatement,resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    public int updatePwd(Connection connection, int id, String password) throws SQLException {
        connection = BaseDao.getConnection();
        String sql = "update smbms_user set userPassword = ? where id = ?";
        PreparedStatement ps = null;
        Object[] params = {password,id};
        int i = BaseDao.execute(connection, sql, params, ps);
        BaseDao.closeResource(null,ps,null);
        return i;
    }
}
